package com.nanyang.tms.controller.saleprice;

import java.util.List;

import javax.validation.Valid;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.nanyang.common.core.annotation.BindParam;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.tms.api.dto.request.saleprice.SaleBaseDiscountAddRequest;
import com.nanyang.tms.api.dto.request.saleprice.SaleQueryRequest;
import com.nanyang.tms.api.dto.request.saleprice.SaleUpdateStateRequest;
import com.nanyang.tms.api.dto.request.saleprice.add.SaleBaseAddRequest;
import com.nanyang.tms.api.dto.request.saleprice.update.SaleBaseUpdateRequest;
import com.nanyang.tms.api.dto.response.saleprice.SaleBaseResponse;
import com.nanyang.tms.api.dto.response.saleprice.SalePageResponse;
import com.nanyang.tms.api.dto.response.saleprice.SalePriceDiscountResponse;
import com.nanyang.tms.api.service.saleprice.SalePriceRpcService;

@RestController
@RequestMapping("/saleprice")
public class SalePriceController {

    @Reference(version = "1.0.0", timeout = 10000, check = false)
    private SalePriceRpcService salePriceRpcService;

    /**
     * 列表
     * @param param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResult<PageBean<SalePageResponse>> listSale(@RequestBody SaleQueryRequest param) {
        ApiResult<PageBean<SalePageResponse>> apiResult = salePriceRpcService.listSale(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 添加
     * @param param
     * @return
     */

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> saveSale(@BindParam @RequestBody @Valid SaleBaseAddRequest param) {
        ApiResult<Boolean> apiResult = salePriceRpcService.saveSale(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 修改
     * @param param
     * @return
     */

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> updateSale(@BindParam @RequestBody @Valid SaleBaseUpdateRequest param) {
        ApiResult<Boolean> apiResult = salePriceRpcService.updateSale(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 禁用
     * @param param
     * @return
     */
    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> disableState(@BindParam @RequestBody @Valid SaleUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = salePriceRpcService.disableState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 启用
     * @param param
     * @return
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> enableState(@BindParam @RequestBody @Valid SaleUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = salePriceRpcService.enableState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * <p>listSupplier</p>
     * <p>Description:查询根据编码</p>
     *
     * @param saleCode
     * @return
     */
    @RequestMapping(value = "/selectByCode/{saleCode}", method = RequestMethod.GET)
    public ApiResult<SaleBaseResponse> selectByCode(@PathVariable("saleCode") String saleCode) {
        ApiResult<SaleBaseResponse> apiResult = salePriceRpcService.selectByCode(saleCode);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * <p>listSupplier</p>
     * <p>Description:查询根据编码会员折扣</p>
     *
     * @param saleProductCode
     * @return
     */
    @RequestMapping(value = "/querySalePriceDiscount/{saleProductCode}", method = RequestMethod.GET)
    public ApiResult<List<SalePriceDiscountResponse>> querySalePriceDiscount(@PathVariable("saleProductCode") String saleProductCode) {
        ApiResult<List<SalePriceDiscountResponse>> apiResult = salePriceRpcService.querySalePriceDiscount(saleProductCode);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 设置会员价
     * @param param
     * @return
     */

    @RequestMapping(value = "/setUpDiscount", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> setUpDiscount(@BindParam @RequestBody @Valid SaleBaseDiscountAddRequest param) {
        ApiResult<Boolean> apiResult = salePriceRpcService.setUpDiscount(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
}
